package com.lemon.smartsafetynew.ui.fragment.notice;

import android.content.Context;
import android.content.res.Resources;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;

import java.lang.reflect.Field;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/7/4.
 */

public class NoticeFragment extends BaseFragment {

    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    private Context context;

    @Override
    protected void getBaseArguments() {
        //TODO
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_notice;
    }

    @Override
    protected void initViews() {


        context = getActivity();

        // ======= 该变指示栏的长度 =======
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 10, 10); //左右各缩进5dp
            }
        });

        // ======= 该变指示栏的长度 =======
        viewPager.setAdapter(new NoticeViewpagerAdapter(getChildFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private class NoticeViewpagerAdapter extends FragmentStatePagerAdapter{

        public NoticeViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return NoticeListFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return 3;  //现在有三个页面
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.notice_tab_1);
                case 1:
                    return getString(R.string.notice_tab_2);
                case 2:
                    return getString(R.string.notice_tab_3);
            }
            return null;
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
            //super.restoreState(state, loader);
        }
    }


    private void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

}
